import grails.converters.JSON
import org.hibernate.FetchMode as FM
class AccdnsController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ accdnsInstanceList: Accdns.list( params ), accdnsInstanceTotal: Accdns.count() ]
        //render Accdns.list( params ) as JSON;
        //render(contentType:"application/json", template:'list',model:[ accdnsInstanceList: Accdns.list( params ), accdnsInstanceTotal: Accdns.count() ])
    }
def customXmlList = {
    params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
    def list = Accdns.list( params )
  render(contentType:"application/json"){
    totalCount(Accdns.count())
    accdns{
      for(a in list){
        accdn(id:a.id, 
            name:a.name,
            dns:a.dns,
            company:a.companySN,
            companySN:a.companySN.companySN,
            callcenterDN:a.companySN.callcenterDN,
            msinum:a.msiinfos.size(),
            policy:a.policy.name+"(控制台编号:"+a.policy.midID+")"
        )
      }
    }
  }
}

    def show = {
        def accdnsInstance = Accdns.get( params.id )

        if(!accdnsInstance) {
            flash.message = "Accdns not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ accdnsInstance : accdnsInstance ] }
    }
    def showAcd = {
        def companysn = Companysn.findByCompanyName("湖北天鹅国旅")
        //def books = author ? Book.findAllByAuthor(author) : []
        def accdnsInstance = companysn ? Accdns.findByCompanySNAndId(companysn,36): []

        if(!accdnsInstance) {
            flash.message = "Accdns not found with id ${params.id}"
            redirect(action:list)
        }
        
        else { println(Accdns.countByCompanySNAndId(companysn,36));return [ accdnsInstance : accdnsInstance ] }
    }

    def delete = {
        def accdnsInstance = Accdns.get( params.id )
        if(accdnsInstance) {
            accdnsInstance.delete()
            flash.message = "Accdns ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Accdns not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def accdnsInstance = Accdns.get( params.id )

        if(!accdnsInstance) {
            flash.message = "Accdns not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ accdnsInstance : accdnsInstance ]
        }
    }

    def update = {
        def accdnsInstance = Accdns.get( params.id )
        if(accdnsInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(accdnsInstance.version > version) {
                    
                    accdnsInstance.errors.rejectValue("version", "accdns.optimistic.locking.failure", "Another user has updated this Accdns while you were editing.")
                    render(view:'edit',model:[accdnsInstance:accdnsInstance])
                    return
                }
            }
            accdnsInstance.properties = params
            if(!accdnsInstance.hasErrors() && accdnsInstance.save()) {
                flash.message = "Accdns ${params.id} updated"
                redirect(action:show,id:accdnsInstance.id)
            }
            else {
                render(view:'edit',model:[accdnsInstance:accdnsInstance])
            }
        }
        else {
            flash.message = "Accdns not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def accdnsInstance = new Accdns()
        accdnsInstance.properties = params
        return ['accdnsInstance':accdnsInstance]
    }

    def save = {
        def accdnsInstance = new Accdns(params)
        if(!accdnsInstance.hasErrors() && accdnsInstance.save()) {
            flash.message = "Accdns ${accdnsInstance.id} created"
            redirect(action:show,id:accdnsInstance.id)
        }
        else {
            render(view:'create',model:[accdnsInstance:accdnsInstance])
        }
    }

        def search={
        SearchCompanysnCommand  searchCmd ->
        def _max = params.limit?params.limit:10
        def _offset = params.start?params.start:0
        def criteria = Accdns.createCriteria() //创建了一个 Hibernate 的 Criteria 查询
        def c = Accdns.createCriteria()
        def branchCount = c.get {
            projections {
                count "id"
            }
            and {
                //if(searchCmd.domain)
                //like ('domain',"%${searchCmd.domain}%")
                //if(searchCmd.callcenterDN)
                //like ('callcenterDN',"%${searchCmd.callcenterDN}%")
                if(searchCmd.companySN)
                {
                companySN{
                like ('companySN',"%${searchCmd.companySN}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.domain)
                {
                companySN{
                like ('domain',"%${searchCmd.domain}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.callcenterDN)
                {
                companySN{
                like ('callcenterDN',"%${searchCmd.callcenterDN}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.companyName)
                {
                companySN{
                like ('companyName',"%${searchCmd.companyName}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }
                //if(searchCmd.direction)
                //eq ('direction',Integer.valueOf("${searchCmd.direction}"))
            }
        }
        def resultList = criteria{
            and {
                //if(searchCmd.domain)
                //like ('domain',"%${searchCmd.domain}%")
                //if(searchCmd.callcenterDN)
                //like ('callcenterDN',"%${searchCmd.callcenterDN}%")
                //if(searchCmd.companyName)
                //like ('companyName',"%${searchCmd.companyName}%")
                if(searchCmd.companySN)
                {
                companySN{
                like ('companySN',"%${searchCmd.companySN}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.domain)
                {
                companySN{
                like ('domain',"%${searchCmd.domain}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.callcenterDN)
                {
                companySN{
                like ('callcenterDN',"%${searchCmd.callcenterDN}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.companyName)
                {
                companySN{
                like ('companyName',"%${searchCmd.companyName}%" )
                }
                fetchMode('companySN', FM.EAGER)
                }

                //如果 teamName 不为空，则与 team 表进行连接，并加入查询条件
                //if(searchCmd.teamName) {
                //team{
                //like ('teamName',"%${searchCmd.teamName}%" )
                //}
                //fetchMode('team', FetchMode.EAGER)
                //}
            }
            maxResults(Integer.valueOf(_max))
            order("id", "asc") //加入了排序的条件，以 id 字段进行升序查询
            firstResult(Integer.valueOf(_offset))
        }


        //flash.message=resultListsum;
        //render resultList as JSON
        render(contentType:"application/json"){
    totalCount(branchCount)
    accdns{
      for(a in resultList){
        accdn(id:a.id,
            name:a.name,
            dns:a.dns,
            company:a.companySN,
            companySN:a.companySN.companySN,
            callcenterDN:a.companySN.callcenterDN,
            domain:a.companySN.domain,
            msinum:a.msiinfos.size(),
            policy:a.policy.name+"(控制台编号:"+a.policy.midID+")"
        )
      }
    }
  }
        //println(searchCmd.companyName);
        //render(view:"list" , model:[accdnsInstanceList: resultList,accdnsInstanceTotal: branchCount])
    }
}
